package com.wu.foodsafetyplayer.app.fragment.main;

import android.graphics.Typeface;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.wu.foodsafetyplayer.R;
import com.wu.foodsafetyplayer.app.fragment.BaseFragment;
import com.wu.foodsafetyplayer.app.fragment.viewpager.MyHouseFragment;
import com.wu.foodsafetyplayer.app.fragment.viewpager.OfficeFragment;
import com.wu.foodsafetyplayer.app.fragment.viewpager.OtherFragment;
import com.wu.foodsafetyplayer.app.fragment.viewpager.ParkingLotFragment;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseFragment {

    private List<BaseFragment> homeFragmentList = new ArrayList<>();
    private int currentPosition = 0;
    private ViewPagerAdapter viewPagerAdapter;
    private ViewPager viewPager;

    RelativeLayout btnMyHome;
    RelativeLayout btnOffice;
    RelativeLayout btnParkingLot;
    RelativeLayout btnOther;

    TextView tvMyHome;
    TextView tvOffice;
    TextView tvParkingLot;
    TextView tvOther;

    View vMyHome;
    View vOffice;
    View vParkingLot;
    View vOther;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        super.initView();

        viewPager = findViewById(R.id.fragment_vp);

        btnMyHome = findViewById(R.id.btn_my_home);
        btnOffice = findViewById(R.id.btn_office);
        btnParkingLot = findViewById(R.id.btn_parking_lot);
        btnOther = findViewById(R.id.btn_other);

        tvMyHome = findViewById(R.id.tv_home);
        tvOffice = findViewById(R.id.tv_office);
        tvParkingLot = findViewById(R.id.tv_parking_lot);
        tvOther = findViewById(R.id.tv_other);

        vMyHome = findViewById(R.id.v_home);
        vOffice = findViewById(R.id.v_office);
        vParkingLot = findViewById(R.id.v_parking_lot);
        vOther = findViewById(R.id.v_other);
        initTable();
    }



    class ViewPagerAdapter extends FragmentPagerAdapter{
        List<BaseFragment> fragmentList;
        public ViewPagerAdapter(@NonNull FragmentManager fm,List<BaseFragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }


        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0){
                return "我的家";
            }else if (position == 1){
                return "办公室";
            }else if (position == 2){
                return "停车场";
            }else {
                return "其他";
            }

        }
        private String makeFragmentName(int viewId,int id){
            return "android:switcher:"+viewId+":"+id;
        }
    }

    private void initTable(){
        tvMyHome.setTextSize(18f);
        tvOffice.setTextSize(14f);
        tvParkingLot.setTextSize(14f);
        tvOther.setTextSize(14f);
        tvMyHome.setTextAppearance(getActivity(), R.style.TabLayoutTextStyle);
        tvOffice.setTextAppearance(getActivity(), Typeface.NORMAL);
        tvParkingLot.setTextAppearance(getActivity(), Typeface.NORMAL);
        tvOther.setTextAppearance(getActivity(), Typeface.NORMAL);
        vMyHome.setVisibility(View.VISIBLE);
        vOffice.setVisibility(View.GONE);
        vParkingLot.setVisibility(View.GONE);
        vOther.setVisibility(View.GONE);

        btnMyHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
            }
        });
        btnOffice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });
        btnParkingLot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2);
            }
        });
        btnOther.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(3);
            }
        });

        homeFragmentList.clear();
        Fragment fragment1 = getActivity().getSupportFragmentManager().findFragmentByTag("android:switcher:fragment:0");
        if (fragment1 != null){
            homeFragmentList.add((BaseFragment) fragment1);
        }else{
            homeFragmentList.add(0,new MyHouseFragment());
        }

        Fragment fragment2 = getActivity().getSupportFragmentManager().findFragmentByTag("android:switcher:fragment:1");
        if (fragment2 != null){
            homeFragmentList.add((BaseFragment) fragment2);
        }else{
            homeFragmentList.add(1,new OfficeFragment());
        }

        Fragment fragment3 = getActivity().getSupportFragmentManager().findFragmentByTag("android:switcher:fragment:2");
        if (fragment3 != null){
            homeFragmentList.add((BaseFragment) fragment3);
        }else{
            homeFragmentList.add(2,new ParkingLotFragment());
        }

        Fragment fragment4 = getActivity().getSupportFragmentManager().findFragmentByTag("android:switcher:fragment:3");
        if (fragment4 != null){
            homeFragmentList.add((BaseFragment) fragment4);
        }else{
            homeFragmentList.add(3,new OtherFragment());
        }

        viewPagerAdapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager(),homeFragmentList);
        viewPager.setOffscreenPageLimit(4);
        viewPager.setAdapter(viewPagerAdapter);

        if (homeFragmentList.size() > currentPosition){
            viewPager.setCurrentItem(currentPosition);
        }else {
            viewPager.setCurrentItem(0);
        }

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewPager.requestLayout();
                if (position == 0){
                    tvMyHome.setTextSize(18f);
                    tvOffice.setTextSize(14f);
                    tvParkingLot.setTextSize(14f);
                    tvOther.setTextSize(14f);
                    tvMyHome.setTextAppearance(getActivity(), R.style.TabLayoutTextStyle);
                    tvOffice.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    tvParkingLot.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    tvOther.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    vMyHome.setVisibility(View.VISIBLE);
                    vOffice.setVisibility(View.GONE);
                    vParkingLot.setVisibility(View.GONE);
                    vOther.setVisibility(View.GONE);
                }else if (position == 1){
                    tvMyHome.setTextSize(14f);
                    tvOffice.setTextSize(18f);
                    tvParkingLot.setTextSize(14f);
                    tvOther.setTextSize(14f);
                    tvMyHome.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    tvOffice.setTextAppearance(getActivity(), R.style.TabLayoutTextStyle);
                    tvParkingLot.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    tvOther.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    vMyHome.setVisibility(View.GONE);
                    vOffice.setVisibility(View.VISIBLE);
                    vParkingLot.setVisibility(View.GONE);
                    vOther.setVisibility(View.GONE);
                }else if (position == 2){
                    tvMyHome.setTextSize(14f);
                    tvOffice.setTextSize(14f);
                    tvParkingLot.setTextSize(18f);
                    tvOther.setTextSize(14f);
                    tvMyHome.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    tvOffice.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    tvParkingLot.setTextAppearance(getActivity(), R.style.TabLayoutTextStyle);
                    tvOther.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    vMyHome.setVisibility(View.GONE);
                    vOffice.setVisibility(View.GONE);
                    vParkingLot.setVisibility(View.VISIBLE);
                    vOther.setVisibility(View.GONE);
                }else {
                    tvMyHome.setTextSize(14f);
                    tvOffice.setTextSize(14f);
                    tvParkingLot.setTextSize(14f);
                    tvOther.setTextSize(18f);
                    tvMyHome.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    tvOffice.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    tvParkingLot.setTextAppearance(getActivity(), R.style.TabLayoutTextStyleNo);
                    tvOther.setTextAppearance(getActivity(), R.style.TabLayoutTextStyle);
                    vMyHome.setVisibility(View.GONE);
                    vOffice.setVisibility(View.GONE);
                    vParkingLot.setVisibility(View.GONE);
                    vOther.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
